from django.http import HttpResponse
from django.shortcuts import render

from article.models import Article


from article.models import User



# Create your views here.


def article_test(request):
    user = User(username="test",password="123456")
    user.save()
    article = Article(title="test",content="test",author=user)
    article.save()

    article = Article.objects.first()

    return HttpResponse(article.author.username)


# def one_to_many(request):
#     user = User.objects.first()
#     articles=user.article_set.all()
#     for article in articles:
#         print(article.title)
#     return HttpResponse("成功！！！")



def query(request):
    # article = Article.objects.filter(id__exact=2)
    #不区分大小写
    # article = Article.objects.filter(title__iexact="Test")
    #包含
    # article = Article.objects.filter(title__contains="test")
    # #包含忽略大小写
    # article = Article.objects.filter(title__icontains="Test")

    #in
    article = Article.objects.filter(id__in = [2])
    # gt 大于 gte 大于等于 lt小于 lte小于等于 startswith 判断字段是否以某个值开始 istartswith 判断字段是否以某个值开始，忽略大小写
    # endswith 判断字段是否以某个值结束 iendswith 判断字段是否以某个值结束，忽略大小写 range 在某个范围
    # date 根据日期查找  year 根据年查找  month 根据月查找 day根据天查找 week_day 根据星期查找  time 根据时间查找
    # isnull 根据空值查找 regex和iregex 正则查找


    print(article.query)
    print(article.first().content)
    return HttpResponse("xxxx")

def query_a(request):
    #查找标题包含text的用户
    user = User.objects.filter(articles__title__contains="test")
    print(user.query)
    print(user.first().username)
    return HttpResponse("xxxx")

